<template>
  <div>
    <!-- 旧件出入库历史查询 -->
    <TopFilter ref="TopFilters" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :fetchapi="fetchApi"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="selectDelearFlag" :title="dialogTitle" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <DealerList @close="Delearclose" @save="Delearsave"></DealerList>
    </BaseDialog>
  </div>
</template>
<script>
// import res from '@/mock/tableData';
import moment from "moment";
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { exportExcelFile } from '@/utils/index';
import { getPartType } from '@/api/dictionary/index.js';
import { getListByPage, getListForExport,exporturl} from '@/api/usedPartsReturnManage/usedPartsReturn_OEM/usedPartsInAndOutHistoryStockQuery';
import { dictionary } from '@/utils/dictMixin';
import { getAppId } from '@/assets/js/auth';
export default {
   mixins: [dictionary],
  components: {
    DealerList
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      selectDelearFlag: false,
      zhuangtaixiugaiFlag: false,
      collapse: true,
      dtlId: null,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      fetchApi: getListByPage,
      fetchParams: {xhrAbort: true}, //ipa 接口
      selectedRows: [], //选中服务站
      dialogTitle: '选择车辆',
      visibleAdd: false
    };
  },
  created() {
    if (getAppId() == 'arcfox') {
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543',[55431001])
    }else{
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543')
    }
    this.fetchParams.beginInDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endInDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.chuqi2();
    this.chuqi3();
  },
  methods: {
    //索赔类型下拉框
    async chuqi2() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5510 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    //出库类型下拉框
    async chuqi3() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5523 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'outType').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'outType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    // 导出
   async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000179' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginInDate|endInDate").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginInDate|endInDate").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '入库开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '入库结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, {xhrAbort: false});
      // obj.beginCreatedAt = this.topFilterInit.find(x => x.fieldName == "beginInDate|endInDate").initialValue[0];
      // obj.endCreatedAt = this.topFilterInit.find(x => x.fieldName == "beginInDate|endInDate").initialValue[1];
      // console.log("=== 点击导出 ===")
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '旧件出入库历史查询');
      // });
    },
    //服务站选择页面
    SelectDelear() {
      this.selectDelearFlag = true;
    }, //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    //保存
    Delearsave(val) {
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.dealer = {...val[0]};
      this.selectDelearFlag = false;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '服务站代码',
          fieldName: 'ownerCode',
          placeholder: '请输入服务站代码...',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.SelectDelear()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '旧件代码', fieldName: 'partCode', placeholder: '请输入旧件代码...', initialValue: '', itemList: [] },
        { type: 'INPUT', label: '旧件名称', fieldName: 'partName', placeholder: '请输入旧件名称...', initialValue: '' },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入VIN...', initialValue: '' },
        { type: 'SELECT', label: '库存状态', fieldName: 'stockStatus', placeholder: '请选择库存状态...', initialValue: '', itemList: [{text: '入库', value: '1'},{text: '出库', value: '2'},{text: '暂存入库', value: '3'},{text: '暂存出库', value: '4'}] },
        { type: 'INPUT', label: '回运通知单号', fieldName: 'returnNo', placeholder: '请输入回运通知单号...', initialValue: '' },
        { type: 'INPUT', label: '出库单号', fieldName: 'outNo', placeholder: '请输入出库单号...', initialValue: '' },
        { type: 'RANGE_DATE', label: '入库日期', fieldName: 'beginInDate|endInDate', placeholder: '请选择日期', initialValue: '', initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }] },
        { type: 'RANGE_DATE', label: '出库日期', fieldName: 'beginOutDate|endOutDate', placeholder: '请选择日期', initialValue: '' },
        { type: 'SELECT', label: '出库类型', fieldName: 'outType', placeholder: '请选择出库类型...', initialValue: '', itemList: [] },
        { type: 'INPUT', label: '主损件供应商', fieldName: 'supCode', placeholder: '请输入主损件供应商...', initialValue: '' },
        { type: 'INPUT', label: '索赔单号', fieldName: 'claimNo', placeholder: '请输入索赔单号...', initialValue: '' },
        { type: 'SELECT', label: '索赔类型', fieldName: 'cmsClaimType', placeholder: '请选择索赔类型...', initialValue: '', itemList: [] },
        { type: 'SELECT', label: '动力形式', fieldName: 'ifComm', placeholder: '请选择动力形式...', initialValue: '',itemList: [] },
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginInDate) {
        this.$notify.warning({ title: '提示信息', message: '入库开始日期不可为空！' });
        return
      }
      if(!val.endInDate) {
        this.$notify.warning({ title: '提示信息', message: '入库结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endInDate).format('YYYY-MM-DD')).diff(moment(val.beginInDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endInDate).format('YYYY-MM-DD')).diff(moment(val.beginInDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val,{xhrAbort: false});
      console.log(val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '库存状态', dataIndex: 'stockStatusStr', sorter: true },
        { title: '出库类型', dataIndex: 'outTypeStr', sorter: true },
        { title: '服务站代码', dataIndex: 'ownerCode', sorter: true },
        { title: '服务站名称', dataIndex: 'dealerName', sorter: true, width: 220 },
        { title: '回运通知单号', dataIndex: 'returnNo', sorter: true },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '车辆属性', dataIndex: 'ifCommStr', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        { title: '入库数量', dataIndex: 'partCount', sorter: true },
        { title: '出库数量', dataIndex: 'outCount', sorter: true },
        { title: '是否主损件', dataIndex: 'isMain', sorter: true },
        { title: '主损件号', dataIndex: 'mainPartCode', sorter: true },
        { title: '主损件名称', dataIndex: 'mainPartName', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true, width: 150 },
        { title: '车型代码', dataIndex: 'modelCode', sorter: true },
        { title: '主损件供应商', dataIndex: 'supName', sorter: true },
        { title: '旧件仓库', dataIndex: 'whName', sorter: true },
        { title: '入库时间', dataIndex: 'inStockDate', sorter: true, width: 140},
        { title: '入库人', dataIndex: 'inBy', sorter: true },
        { title: '入库说明', dataIndex: 'inInfoStr', sorter: true },
        { title: '出库单号', dataIndex: 'outNo', sorter: true },
        { title: '出库时间', dataIndex: 'outDate', sorter: true, width: 140},
        // { title: '出库类型', dataIndex: 'outTypeStr', sorter: true },
        { title: '领件人', dataIndex: 'outTo', sorter: true },
        { title: '联系电话', dataIndex: 'outPhone', sorter: true },
        { title: '出库人', dataIndex: 'outBy', sorter: true },
        // { title: '出库说明', dataIndex: 'inInfoStr', sorter: true },
      ];
    }
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
